import dayjs from 'dayjs';
import { readBody } from 'h3';
import { AdminModel } from '~/server/models/admin.model';

// 修改资料接口，仅支持修改昵称和头像
export default defineEventHandler(async (event): Promise<any> => {
	try {
		const body = await readBody(event);

		const { _id, nickname, avatar } = body;

		if (!_id) return { code: 0, message: '缺少 _id' };

		const updateData: any = { updatedAt: dayjs().format('YYYY年MM月DD日 HH:mm:ss') };

		if (nickname !== undefined) updateData.nickname = nickname;

		if (avatar !== undefined) updateData.avatar = avatar;

		await AdminModel.findByIdAndUpdate(_id, updateData);

		return { code: 1, message: '资料修改成功' };
	} catch (error: any) {
		return { code: 0, message: error.message };
	}
});
